package com.hzhh123.download.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hzhh123.download.R;
import com.hzhh123.download.model.File;

import java.util.List;

public class FileDownloadCompleteAdapter extends RecyclerView.Adapter<FileDownloadCompleteAdapter.ViewHolder> {
    private List<File> list;
    private Context context;

    public FileDownloadCompleteAdapter(List<File> list,  Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_file_down_complete, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder,final int i) {
        File file=list.get(i);
        viewHolder.mFileName.setText(file.getFileName());
        if(file.getFileType().equals(".doc")||file.getFileType().equals(".docx")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.ppt));
        }else if(file.getFileType().equals(".xls")||file.getFileType().equals(".xlsx")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.xls));
        }else if(file.getFileType().equals(".txt")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.xls));
        }else if(file.getFileType().equals(".pdf")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.pdf));
        }else if(file.getFileType().equals(".mp3")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.mp3));
        }else if(file.getFileType().equals(".mp4")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.mp4));
        }else if(file.getFileType().equals(".zip")||file.getFileType().equals(".rar")||file.getFileType().equals(".tar.gz")||file.getFileType().equals(".tar")||
                file.getFileType().equals(".7z")){
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.zip));
        }else{
            viewHolder.img.setImageDrawable(context.getResources().getDrawable(R.mipmap.file));
        }
        viewHolder.mCreateTime.setText(DateFormat.format("yyyy/MM/dd",file.getCreateTime()));
        viewHolder.mSize.setText(file.getSizeStr());
        if(file.isShow()) {
            viewHolder.mCheckbox.setVisibility(View.VISIBLE);
            if(file.isChecked()){
                viewHolder.mCheckbox.setChecked(true);
            }else{
                viewHolder.mCheckbox.setChecked(false);
            }
        }else{
            viewHolder.mCheckbox.setVisibility(View.GONE);
            viewHolder.mCheckbox.setChecked(false);
        }
        viewHolder.mCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,i);
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,i);
            }
        });
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.onItemLongClick(v,i);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mFileName;
        public ImageView img;
        public TextView mCreateTime;
        public TextView mSize;
        public CheckBox mCheckbox;

        public ViewHolder(View itemView) {
            super(itemView);
            mFileName = itemView.findViewById(R.id.file_name);
            mSize = itemView.findViewById(R.id.size);
            mCreateTime = itemView.findViewById(R.id.create_time);
            img=itemView.findViewById(R.id.img);
            mCheckbox=itemView.findViewById(R.id.checkbox);
        }
    }

    //点击 RecyclerView 某条的监听
    public interface OnItemClickListener{

        /**
         * 当RecyclerView某个被点击的时候回调
         * @param view 点击item的视图
         */
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);

    }

    private FileDownloadCompleteAdapter.OnItemClickListener onItemClickListener;

    /**
     * 设置RecyclerView某个的监听
     * @param onItemClickListener
     */
    public void setOnItemClickListener(FileDownloadCompleteAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

}

